package com.aaa.marketresearch.android.widget;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import com.aaa.marketresearch.android.app.BillContentScreen;
import com.aaa.marketresearch.android.app.OfferEditScreen;
import com.aaa.marketresearch.android.app.R;
import com.aaa.marketresearch.android.data.UserInfo;
import com.aaa.marketresearch.android.data.domain.BillContent;
import com.aaa.marketresearch.android.data.domain.Offer;
import com.aaa.marketresearch.android.util.SimpleLog;

import java.util.List;

/**
 * @author Nail
 */
public class OfferAdapter extends BaseAdapter {

    private Context context;
    LayoutInflater lInflater;
    List<Offer> offers;

    public OfferAdapter(Context context, List<Offer> offers) {
        this.context = context;
        this.offers = offers;
        this.lInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return offers.size();
    }

    @Override
    public Object getItem(int position) {
        return this.offers.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    // товар по позиции
    Offer getOffer(int position) {
        return ((Offer) getItem(position));
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // используем созданные, но не используемые view
        View view = convertView;
        if (view == null) {
            view = lInflater.inflate(R.layout.offer_view, parent, false);
        }

        final Offer offer = this.getOffer(position);

        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SimpleLog.info("chooseOfferButton");

                UserInfo userInfo = UserInfo.getInstance();
                final BillContent billContent = userInfo.getCurrentBillContent().makeSame();
                billContent.setOffer(offer);
                userInfo.setCurrentBillContent(billContent);

                Intent intent = new Intent(OfferAdapter.this.context, BillContentScreen.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
                OfferAdapter.this.context.startActivity(intent);
            }
        });


        ((TextView) view.findViewById(R.id.offerName)).setText(offer.getName());

        Button editButton = (Button) view.findViewById(R.id.editOffer);
        editButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SimpleLog.info("editOfferButton");

                UserInfo userInfo = UserInfo.getInstance();
                userInfo.getCurrentBillContent().setOffer(offer);

                Intent intent = new Intent(OfferAdapter.this.context, OfferEditScreen.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
                OfferAdapter.this.context.startActivity(intent);
            }
        });

        return view;
    }
}